/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * ExternalFileReader Class:
 * This class is used to read an external XML file from disk, that contains all the relevant data such that the program can run.
 * @author Vale
 *
 */
public class ExternalFileReader {

    private static final String FILE_NAME = "E:\\SCMSInfo.xml";
    private static final int QUALITY_MAX = 5;
    private static final double INVCOST_BUNS = 0.30;
    private static final int INVSTK_BUNS = 400;
    private static final double INVCOST_LETTUCE = 0.40;
    private static final int INVSTK_LETTUCE = 100;
    private static final double INVCOST_TOM = 0.80;
    private static final int INVSTK_TOM = 200;
    private static final double INVCOST_CCPAT = 0.80;
    private static final int INVSTK_CCPAT = 150;
    private static final double INVCOST_MCPAT = 0.65;
    private static final int INVSTK_MCPAT = 250;
    private static final double INVCOST_BPAT = 0.45;
    private static final int INVSTK_BPAT = 800;
    private static final double INVCOST_TSAU = 1.00;
    private static final int INVSTK_TSAU = 50;
    private static final double INVCOST_MAYO = 1.40;
    private static final int INVSTK_MAYO = 30;
    private static final double INVCOST_CHEESE = 1.20;
    private static final int INVSTK_CHEESE = 20;
    private static final double INVCOST_PBUN = 0.15;
    private static final int INVSTK_PBUN = 300;
    private static final double INVCOST_ONI = 1.30;
    private static final int INVSTK_ONI = 50;
    private static final double PRODCOST_BM = 4.95;
    private static final int PRODSCHED_BM = 100;
    private static final double PRODCOST_CC = 5.95;
    private static final int PRODSCHED_CC = 200;
    private static final double PRODCOST_MC = 5.45;
    private static final int PRODSCHED_MC = 120;
    private static final double PRODCOST_CH = 2.40;
    private static final int PRODSCHED_CH = 500;
    private static final double PRODCOST_QP = 5.45;
    private static final int PRODSCHED_QP = 150;
    private ProductList products;
    private InventoryList inventory;
    private Product currProd;

    /**
     * ExternalFileReader Constructor:
     * This constructor is used to read the product list and inventory lsit information from file and save it to the product list and inventory list.
     *
     */
    public ExternalFileReader(ProductList prodList, InventoryList invList) throws IOException {
        products = prodList;
        inventory = invList;
        int prodItems;
        int invItems;
        int invSize;
        XMLDecoder readFile = null;
        try {
            FileInputStream fis = new FileInputStream(FILE_NAME);
            BufferedInputStream bis = new BufferedInputStream(fis);
            readFile = new XMLDecoder(new BufferedInputStream(new FileInputStream(FILE_NAME)));
        } catch (FileNotFoundException ex) {
            File externalFile = new File(FILE_NAME);
            externalFile.createNewFile();
            createTestData();
        }
        prodItems = (Integer) readFile.readObject();
        invItems = (Integer) readFile.readObject();
        for (int i = 0; i < prodItems; i++) {
            currProd = new Product();
            currProd.setProdName((String) readFile.readObject());
            currProd.setProdCost((Double) readFile.readObject());
            currProd.setProdSched((Integer) readFile.readObject());
            invSize = Integer.parseInt(readFile.readObject().toString());
            for (int j = 0; j < invSize; j++) {
                Inventory currProdInv = new Inventory();
                currProdInv.setInvName((String) readFile.readObject());
                currProdInv.setInvCost((Double) readFile.readObject());
                currProdInv.setInvQuality((Integer) readFile.readObject());
                currProdInv.setInvUnits((String) readFile.readObject());
                currProdInv.setInvStock((Integer) readFile.readObject());
                currProd.getInventList().add(currProdInv);
            }
            products.add(currProd);
        }

        for (int k = 0; k < invItems; k++) {
            Inventory currInvItem = new Inventory();
            currInvItem.setInvName((String) readFile.readObject());
            currInvItem.setInvCost((Double) readFile.readObject());
            currInvItem.setInvQuality((Integer) readFile.readObject());
            currInvItem.setInvUnits((String) readFile.readObject());
            currInvItem.setInvStock((Integer) readFile.readObject());
            inventory.add(currInvItem);
        }

        readFile.close();
    }

    /**
     * void createTestData() Method:
     * Used to set up the test data if it does not exist.
     */
    public void createTestData() throws IOException {
        Product prodItem;

        //Create new Inventory Items




        //Inventory Item 1 - Buns
        Inventory ssBun = new Inventory();
        ssBun.setInvName("Sesame Seed Buns");
        ssBun.setInvQuality((int) (Math.random()*QUALITY_MAX));
        ssBun.setInvCost(INVCOST_BUNS);
        ssBun.setInvUnits("Whole Buns");
        ssBun.setInvStock(INVSTK_BUNS);
        inventory.add(ssBun);

        //Inventory Item 2 - Lettuce
        Inventory lettuce = new Inventory();
        lettuce.setInvName("Shredded Lettuce");
        lettuce.setInvQuality((int) (Math.random()*QUALITY_MAX));
        lettuce.setInvCost(INVCOST_LETTUCE);
        lettuce.setInvUnits("kg");
        lettuce.setInvStock(INVSTK_LETTUCE);
        inventory.add(lettuce);

        //Inventory Item 3 - Tomato
        Inventory tomato = new Inventory();
        tomato.setInvName("Sliced Tomato");
        tomato.setInvQuality((int) (Math.random()*QUALITY_MAX));
        tomato.setInvCost(INVCOST_TOM);
        tomato.setInvUnits("kg");
        tomato.setInvStock(INVSTK_TOM);
        inventory.add(tomato);

        //Inventory Item 4 - Crispy Chicken Patty
        Inventory ccPatty = new Inventory();
        ccPatty.setInvName("Crispy Chicken Patty");
        ccPatty.setInvQuality((int) (Math.random()*QUALITY_MAX));
        ccPatty.setInvCost(INVCOST_CCPAT);
        ccPatty.setInvUnits("Patties");
        ccPatty.setInvStock(INVSTK_CCPAT);
        inventory.add(ccPatty);

        //Inventory Item 5 - McChicken Patty
        Inventory mcPatty = new Inventory();
        mcPatty.setInvName("McChicken Patty");
        mcPatty.setInvQuality((int) (Math.random()*QUALITY_MAX));
        mcPatty.setInvCost(INVCOST_MCPAT);
        mcPatty.setInvUnits("Patties");
        mcPatty.setInvStock(INVSTK_MCPAT);
        inventory.add(mcPatty);

        //Inventory Item 6 - Beef Patty
        Inventory bPatty = new Inventory();
        bPatty.setInvName("Beef Patty");
        bPatty.setInvQuality((int) (Math.random()*QUALITY_MAX));
        bPatty.setInvCost(INVCOST_BPAT);
        bPatty.setInvUnits("Patties");
        bPatty.setInvStock(INVSTK_BPAT);
        inventory.add(bPatty);

        //Inventory Item 7 - Tomato Sauce
        Inventory tSauce = new Inventory();
        tSauce.setInvName("Tomato Sauce");
        tSauce.setInvQuality((int) (Math.random()*QUALITY_MAX));
        tSauce.setInvCost(INVCOST_TSAU);
        tSauce.setInvUnits("Litre");
        tSauce.setInvStock(INVSTK_TSAU);
        inventory.add(tSauce);

        //Inventory Item 8 - Mayonaise
        Inventory mayo = new Inventory();
        mayo.setInvName("Mayonaise");
        mayo.setInvQuality((int) (Math.random()*QUALITY_MAX));
        mayo.setInvCost(INVCOST_MAYO);
        mayo.setInvUnits("Litre");
        mayo.setInvStock(INVSTK_MAYO);
        inventory.add(mayo);

        //Inventory Item 9 - Cheese
        Inventory cheese = new Inventory();
        cheese.setInvName("Cheese Slice");
        cheese.setInvQuality((int) (Math.random()*QUALITY_MAX));
        cheese.setInvCost(INVCOST_CHEESE);
        cheese.setInvUnits("kg");
        cheese.setInvStock(INVSTK_CHEESE);
        inventory.add(cheese);

        //Inventory Item 10 - Plain Bun
        Inventory pBun = new Inventory();
        pBun.setInvName("Plain Bun");
        pBun.setInvQuality((int) (Math.random()*QUALITY_MAX));
        pBun.setInvCost(INVCOST_PBUN);
        pBun.setInvUnits("Whole Buns");
        pBun.setInvStock(INVSTK_PBUN);
        inventory.add(pBun);

        //Inventory Item 11 - Onion
        Inventory onion = new Inventory();
        onion.setInvName("Diced Onion");
        onion.setInvQuality((int) (Math.random()*QUALITY_MAX));
        onion.setInvCost(INVCOST_ONI);
        onion.setInvUnits("kg");
        onion.setInvStock(INVSTK_ONI);
        inventory.add(onion);

        //Create new Products

        //Product 1 - Big Mac
        Product bMac = new Product();
        bMac.setProdName("Big Mac Burger");
        bMac.setProdCost(PRODCOST_BM);
        bMac.setProdSched(PRODSCHED_BM);
        bMac.getInventList().add(ssBun);
        bMac.getInventList().add(lettuce);
        bMac.getInventList().add(cheese);
        bMac.getInventList().add(cheese);
        bMac.getInventList().add(bPatty);
        bMac.getInventList().add(bPatty);
        bMac.getInventList().add(mayo);
        products.add(bMac);

        //Product 2 - Cripsy Chicken Burger
        Product ccBurg = new Product();
        ccBurg.setProdName("Crispy Chicken Burger");
        ccBurg.setProdCost(PRODCOST_CC);
        ccBurg.setProdSched(PRODSCHED_CC);
        ccBurg.getInventList().add(pBun);
        ccBurg.getInventList().add(lettuce);
        ccBurg.getInventList().add(ccPatty);
        ccBurg.getInventList().add(mayo);
        products.add(ccBurg);

        //Product 3 - McChicken Burger
        Product mcBurg = new Product();
        mcBurg.setProdName("McChicken Burger");
        mcBurg.setProdCost(PRODCOST_MC);
        mcBurg.setProdSched(PRODSCHED_MC);
        mcBurg.getInventList().add(ssBun);
        mcBurg.getInventList().add(lettuce);
        mcBurg.getInventList().add(mcPatty);
        mcBurg.getInventList().add(mayo);
        products.add(mcBurg);

        //Product 4 - Cheeseburger
        Product cBurg = new Product();
        cBurg.setProdName("Cheeseburger");
        cBurg.setProdCost(PRODCOST_CH);
        cBurg.setProdSched(PRODSCHED_CH);
        cBurg.getInventList().add(pBun);
        cBurg.getInventList().add(cheese);
        cBurg.getInventList().add(bPatty);
        cBurg.getInventList().add(tSauce);
        cBurg.getInventList().add(onion);
        products.add(cBurg);

        //Product 5 - Quarter Pounder
        Product qp = new Product();
        qp.setProdName("Quarter Pounder");
        qp.setProdCost(PRODCOST_QP);
        qp.setProdSched(PRODSCHED_QP);
        qp.getInventList().add(ssBun);
        qp.getInventList().add(tSauce);
        qp.getInventList().add(onion);
        qp.getInventList().add(bPatty);
        qp.getInventList().add(bPatty);
        qp.getInventList().add(cheese);
        products.add(qp);

        // Write to File
        new ExternalFileWriter(products, inventory);
    }
}
